Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

feat: Add ToPointer helper function to convert values to pointers #427

Merged
merged 1 commit into from
Jul 21, 2022

Conversation

hermanschaaf
Copy link
Member

@hermanschaaf hermanschaaf commented Jul 20, 2022

This adds a new helper function that will be useful in automatically generated resolver functions, where we can't always be sure whether the passed in type will be a pointer or a value. With this helper, we can force it to always be a pointer and perform a safe typecast afterwards.

Example usage:

func fetchBasePointerRelationInnerRelations(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
	r := helpers.ToPointer(parent.Item).(*tests.PointerRelationStruct)
        // rest of function...
}

Alternatively, if we didn't have this function, we could do:

func fetchBasePointerRelationInnerRelations(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
	var r *tests.PointerRelationStruct
	switch v := parent.Item.(type) {
	case tests.PointerRelationStruct:
		r = &v
	case *tests.PointerRelationStruct:
		r = v
	}
        // rest of function...

IMO it's nice to have this helper so that the resolvers can remain minimal.

@hermanschaaf hermanschaaf requested a review from a team as a code owner July 20, 2022 15:53
@hermanschaaf hermanschaaf requested review from candiduslynx and roneli and removed request for a team July 20, 2022 15:53
@hermanschaaf hermanschaaf changed the title feat: add ToPointer helper function to convert values to pointers feat: Add ToPointer helper function to convert values to pointers Jul 20, 2022
@hermanschaaf hermanschaaf merged commit e34dcfb into cloudquery:main Jul 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants